package com.memoir.jpa.repository;

import com.memoir.jpa.model.TodoDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ldm
 * @since 2017-11-21
 */

//假如写一个repository类的话，要实现顶级接口repository
//假如写一个接口的话，直接集成jpaRepository即可

@Repository
public interface TodoRepository  extends TodoExtendRepository,JpaRepository<TodoDto,Integer> {

//    @Async
//    @CreatedBy
//    @CreatedDate
//    @LastModifiedBy
//    @LastModifiedDate
//    @EnableJpaAuditing
//    @JsonIgnore
//    @JsonIgnoreProperties()
//    @JsonIgnoreType
//    @Lock(LockModeType.READ)

    //方法名约定
    public List<TodoDto> getTodoDtosByTitleContaining(String title);

    //普通sql
    @Query(value = "select * from t_todo t where t.title like ?1",nativeQuery = true)    //false就可以用jpql
    public List<TodoDto> findTodosByTitle(String title);

    //jpql
    //@Query(value = "select t from TodoDto t where t.title like %?1%")
    //public List<TodoDto> readTodosByTitle(String title);

    ////jpql
//    @Query(value = "select t from TodoDto t where t.title like %:title%")
//    public List<TodoDto> readTodosByTitle(@Param("title") String title);

    //SPEL表达式
    @Query(value = "select t from TodoDto t where t.title = :#{#todoDto.title}")
    public List<TodoDto> readTodosByTitle(TodoDto todoDto);

    @Transactional
    @Modifying
    @Query(value = "update TodoDto t  set t.title = :title where t.id = :id")   //用modify必须要有事物
    public int updateTodo(@Param("title") String title,@Param("id") int id);

}
